/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prerecieve.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.entity.taccount.Taccount;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.base.service.taccount.TaccountService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.financing.service.finserviceorder.YYService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.prepay.entity.TprepaymentActual;
import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedActual;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedEstimated;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.prerecieve.service.TadvancesReceivedService;

import java.util.ArrayList;
import java.util.List;

/**
 * 预收款Controller
 * @author wsm
 * @version 2021-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/prerecieve/tadvancesReceived")
public class TadvancesReceivedController extends BaseController {

	@Autowired
	private TadvancesReceivedService tadvancesReceivedService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private FixedExpenseService fixedExpenseService;
	@Autowired
	private YYService yyService;
	@Autowired
	private TaccountService taccountService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TadvancesReceived get(String advancesReceivedId, boolean isNewRecord) {
		return tadvancesReceivedService.get(advancesReceivedId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = {"list", ""})
	public String list(TadvancesReceived tadvancesReceived, Model model) {
		//客户
		List<Customer> customerList = customerService.findList(new Customer());
		model.addAttribute("customerList",customerList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);
		model.addAttribute("tadvancesReceived", tadvancesReceived);
		return "modules/prerecieve/tadvancesReceivedList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TadvancesReceived> listData(TadvancesReceived tadvancesReceived, HttpServletRequest request, HttpServletResponse response) {
		tadvancesReceived.setPage(new Page<>(request, response));
		Page<TadvancesReceived> page = tadvancesReceivedService.findPage(tadvancesReceived);
	/*	for (TadvancesReceived tar1 : page.getList()) {
			if(tar1.getParentBillType()!=null&&
					!"".equals(tar1.getParentBillType())
					&&tar1.getParentBillCode()!=null&&!"".equals(tar1.getParentBillCode())) {
				String billCodeById = yyService.getBillCodeById(tar1.getParentBillType(), tar1.getParentBillCode());
				tar1.setParentBillCode1(tar1.getParentBillCode());
				tar1.setParentBillCode(billCodeById);
			}
		}*/
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "form")
	public String form(TadvancesReceived tadvancesReceived, Model model) {

		if(tadvancesReceived.getIsNewRecord()){
			model.addAttribute("rowNumber", ListUtils.newArrayList());
		} else {
			List<TadvancesReceivedEstimated> rowNumber =new ArrayList<>();
			for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {
				rowNumber.add(tadvancesReceivedEstimated);
			}
			model.addAttribute("rowNumber", rowNumber);
		}


		//付款方式
		List<FixedExpense> fixedExpenseList=fixedExpenseService.findList(new FixedExpense());
		model.addAttribute("fixedExpenseList",fixedExpenseList);
		//客户
		List<Customer> customerList = customerService.findList(new Customer());
		model.addAttribute("customerList",customerList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);

		model.addAttribute("tadvancesReceived", tadvancesReceived);
		return "modules/prerecieve/tadvancesReceivedForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "detail")
	public String detail(TadvancesReceived tadvancesReceived, Model model) {
		//赋值字段类型名称
		for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {
			tadvancesReceivedEstimated.setAdvancesReceivedTypeName(tadvancesReceivedEstimated.getFixedExpense().getFixedExpenseName());
		}

		for (TadvancesReceivedActual tadvancesReceivedActual : tadvancesReceived.getTadvancesReceivedActualList()) {
			tadvancesReceivedActual.setAdvancesReceivedTypeName(tadvancesReceivedActual.getFixedExpense().getFixedExpenseName());
			tadvancesReceivedActual.setAdvancesReceivedBankName(tadvancesReceivedActual.getTaccount().getAccountName());
		}

		//行号
		ServiceOrder serviceOrder1 =new ServiceOrder();
		serviceOrder1.setServiceOrderId(tadvancesReceived.getServiceOrderId());
		serviceOrder1=serviceOrderService.get(serviceOrder1);
		List<Long> numberList =new ArrayList<>();
		for (ServiceOrderDetail serviceOrderDetail : serviceOrder1.getServiceOrderDetailList()) {
			numberList.add(serviceOrderDetail.getRowNumber());
		}
		model.addAttribute("numberList",numberList);
		//银行
		Taccount taccount =new Taccount();
		taccount.setIsBank("1");
		List<Taccount> taccountList = taccountService.findList(taccount);
		model.addAttribute("taccountList",taccountList);
		//客户
		List<Customer> customerList = new ArrayList<>();
		//付款方式
		List<FixedExpense> fixedExpenseList=fixedExpenseService.findList(new FixedExpense());
		model.addAttribute("fixedExpenseList",fixedExpenseList);
		//服务订单
		List<ServiceOrder> serviceOrderList = new ArrayList<>();
		Customer customer =new Customer();
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(tadvancesReceived.getServiceOrderId());
		customer.setCustomerId(tadvancesReceived.getCustomerId());
		customer= customerService.get(customer);
		serviceOrder=serviceOrderService.get(serviceOrder);

		serviceOrderList.add(serviceOrder);
		customerList.add(customer);
		if(tadvancesReceived.getParentBillType()!=null&&tadvancesReceived.getParentBillCode()!=null
				&&!"".equals(tadvancesReceived.getParentBillType())&&!"".equals(tadvancesReceived.getParentBillCode())) {
			String billCodeById = yyService.getBillCodeById(tadvancesReceived.getParentBillType(), tadvancesReceived.getParentBillCode());
			tadvancesReceived.setParentBillCode1(billCodeById);
		}
		model.addAttribute("customerList",customerList);
		model.addAttribute("serviceOrderList",serviceOrderList);
		model.addAttribute("tadvancesReceived", tadvancesReceived);
		return "modules/prerecieve/tadvancesReceivedDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TadvancesReceived tadvancesReceived) {

		    tadvancesReceivedService.save(tadvancesReceived);

		return renderResult(Global.TRUE, text("保存预收款成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("prerecieve:tadvancesReceived:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TadvancesReceived tadvancesReceived) {
		tadvancesReceivedService.delete(tadvancesReceived);
		return renderResult(Global.TRUE, text("删除预收款成功！"));
	}


	//找寻对应的预收款单
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "findPreRec1")
	@ResponseBody
	public List<TadvancesReceivedEstimated> findPreRec(TadvancesReceived tadvancesReceived,String serviceorder,String cust) {

		List<TadvancesReceivedEstimated>  list= tadvancesReceivedService.findListBy(cust,serviceorder);

		return list;
	}
	//找寻对应的预收款单
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "findPreRec")
	@ResponseBody
	public List<TadvancesReceivedEstimated> findPreRec1(TadvancesReceived tadvancesReceived,String serviceorder,String cust) {
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderCode(serviceorder);
		serviceOrder = serviceOrderService.findList(serviceOrder).get(0);

		List<TadvancesReceivedEstimated>  list= tadvancesReceivedService.findListBy(cust,serviceOrder.getServiceOrderId());

		return list;
	}
	//找寻对应的预收款单的余额
	@RequiresPermissions("prerecieve:tadvancesReceived:view")
	@RequestMapping(value = "findLeftMoney")
	@ResponseBody
	public Double findLeftMoney(String value ,String type,Long number) {

		Double d =null;
		TadvancesReceived tadvancesReceived = new TadvancesReceived();
		tadvancesReceived.setAdvancesReceivedCode(value);
		tadvancesReceived =tadvancesReceivedService.findList(tadvancesReceived).get(0);
		tadvancesReceived=tadvancesReceivedService.get(tadvancesReceived);

		for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()) {
			if(tadvancesReceivedEstimated.getRowNumber().equals(number)&&tadvancesReceivedEstimated.getAdvancesReceivedType().equals(type)){
				d= tadvancesReceivedEstimated.getUsedAmount();
			}
		}
		return d;
	}

	/**
	 * 财务确认
	 * @param apId
	 * @return
	 */
	@RequestMapping(value = "updateFinancialRecognition")
	@ResponseBody
	public String  updateFinancialRecognition(@RequestParam("apId") String apId, @RequestParam("sta") String sta){
		String str = "";
		int i = tadvancesReceivedService.updateFinancialRecognition(apId,sta);
		if (i > 0){
			str = "操作成功";
		}
		return str;
	}
}